home *** CD-ROM | disk | FTP | other *** search
- { uprint.pas -- Printer support unit for text and graphics }
-
- unit UPrint;
-
- interface
-
- uses WinTypes, WinProcs, WObjects, Strings;
-
- function NextToken(P: PChar; C: Char): PChar;
- function PrnStart(DocumentName: PChar): Boolean;
- procedure NewPage;
- procedure PrnLine(P: PChar);
- procedure PrnStop;
-
- var
-
- PDc: HDC; { Printer's DC: valid if PrnStart = true }
-
- implementation
-
- const
-
- LeftMargin = 10; { Width of page's left margin }
- LinesAtTop = 3; { Number of lines in page's top margin }
- LinesAtBottom = 3; { Number of lines in page's bottom margin }
- MinimumLines = LinesAtTop + LinesAtBottom + 1;
-
- var
-
- Printing: Boolean; { True after successful call to PrnStart }
- EscResult: Integer; { Result of most recent call to Escape }
- LineHeight: Integer; { Height of each line in printer units }
- CurrentLine: Integer; { Line number on page. 0 = at top of page }
- LinesPerPage: Integer; { Maximum number of lines printed per page }
-
-
- {- Return pointer to next token in P or previous P if P = nil }
- function NextToken(P: PChar; C: Char): PChar;
- const
- Next: PChar = nil;
- begin
- if P = nil then P := Next;
- Next := StrScan(P, C);
- if Next <> nil then
- begin
- Next^ := #0;
- Next := @Next[1]
- end;
- NextToken := P
- end;
-
- {- Initialize global printing parameters }
- procedure InitPrintParams;
- var
- TM: TTextMetric;
- PageWidth, PageHeight: Integer;
- begin
- GetTextMetrics(PDc, TM);
- PageWidth := GetDeviceCaps(PDc, HorzRes); { Not used }
- PageHeight := GetDeviceCaps(PDc, VertRes);
- LineHeight := TM.tmHeight + TM.tmHeight div 2;
- if LineHeight <= 0 then
- LineHeight := 10; { Prevent divide by zero error }
- LinesPerPage := PageHeight div LineHeight;
- if LinesPerPage < MinimumLines then
- LinesPerPage := MinimumLines;
- CurrentLine := LinesAtTop
- end;
-
-
- { Global routines }
-
- {- If true, text may be printed by calling PrnLine. }
- function PrnStart(DocumentName: PChar): Boolean;
- var
- Buffer: array[0 .. 80] of Char;
- DriverName, DeviceName, OutputName: PChar;
- begin
- GetProfileString('windows', 'device', ',,', Buffer, Sizeof(Buffer));
- DeviceName := NextToken(Buffer, ',');
- DriverName := NextToken(nil, ',');
- OutputName := NextToken(nil, ',');
- PDc := CreateDC(DriverName, DeviceName, OutputName, nil);
- if PDc <> 0 then
- begin
- EscResult := Escape(PDc, StartDoc, StrLen(DocumentName),
- DocumentName, nil);
- Printing := EscResult > 0
- end else
- Printing := false;
- if Printing then
- begin
- SetCursor(LoadCursor(0, idc_Wait));
- InitPrintParams
- end else
- MessageBox(Application^.MainWindow^.HWindow,
- 'Printer initialization failed', 'Error',
- mb_IconExclamation or mb_Ok);
- PrnStart := Printing
- end;
-
- {- Print current page and start a new one }
- procedure NewPage;
- begin
- if Printing and (EscResult > 0) then
- begin
- EscResult := Escape(PDc, NewFrame, 0, nil, nil);
- CurrentLine := LinesAtTop
- end
- end;
-
- {- Print one line addressed by P }
- procedure PrnLine(P: PChar);
- begin
- Inc(CurrentLine);
- TextOut(PDc, LeftMargin, CurrentLine * LineHeight, P, StrLen(P));
- if CurrentLine >= LinesPerPage - LinesAtBottom then
- NewPage
- end;
-
- {- Call only if PrnStop returned true. }
- procedure PrnStop;
- begin
- if Printing then
- begin
- if CurrentLine > LinesAtTop then
- NewPage; { Print last partial page }
- if EscResult > 0 then
- Escape(PDc, EndDoc, 0, nil, nil);
- SetCursor(LoadCursor(0, idc_Arrow));
- DeleteDC(PDc);
- Printing := false
- end
- end;
-
- end.
-
-
- {--------------------------------------------------------------
- Copyright (c) 1991 by Tom Swan. All rights reserved.
- Revision 1.00 Date: 5/16/1991
- ---------------------------------------------------------------}
-